package com.epalmpay.controller.prepurchase;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.epalmpay.commom.BaseController;
import com.epalmpay.exception.ApplicationException;
import com.epalmpay.status.Rescode;
import com.epalmpay.dto.prepurchase.DoAddPrePurchBalanceInDTO;
import com.epalmpay.dto.prepurchase.PreBalanceChangeInDTO;
import com.epalmpay.dto.prepurchase.PreBalanceChangeListOutDTO;
import com.epalmpay.dto.prepurchase.PrePurActivityListOutDTO;
import com.epalmpay.dto.prepurchase.PrePurActivityMoreInfoOutDTO;
import com.epalmpay.dto.prepurchase.PrePurActivityStaticListOutDTO;
import com.epalmpay.dto.prepurchase.PrePurApproveInDTO;
import com.epalmpay.dto.prepurchase.PrePurInDTO;
import com.epalmpay.dto.prepurchase.PreTradeOutDTO;
import com.epalmpay.dto.prepurchase.PrepurchaseInfoInDTO;
import com.epalmpay.dto.prepurchase.ShopPrePurListOutDTO;
import com.epalmpay.dto.prepurchase.UpdateInPrepurInDTO;
import com.epalmpay.dto.system.BaseOutDTO;
import com.epalmpay.dto.system.PageDTO;
import com.epalmpay.entity.Page;
import com.epalmpay.service.prepurchase.IPrePurchaseService;
import com.epalmpay.util.Tools;

@Controller
@RequestMapping(value = "/prePurchase")
public class PrePurchaseController  extends BaseController{

	@Resource
	private IPrePurchaseService prePurchaseService;

	// 预采购活动 添加
	@RequestMapping(value = "/savePrePurActivity")
	@ResponseBody
	public BaseOutDTO savePrePurchaseActivity(PrepurchaseInfoInDTO prePurInDTO) {
		BaseOutDTO baseOutDTO = new BaseOutDTO();
		try {

			return prePurchaseService.addPrePurchaseActivity(prePurInDTO);

		} catch (ApplicationException ae) {
			baseOutDTO.setErrorCode(ae.errCode, ae.errMsg);
			ae.printStackTrace();
		} catch (Exception e) {
			baseOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
			e.printStackTrace();
		}
		return baseOutDTO;
	}

	// 预采购活动 添加
	@RequestMapping(value = "/doEditPrePurActivity")
	@ResponseBody
	public BaseOutDTO doEditPrePurActivity(PrepurchaseInfoInDTO prePurInDTO) {
		BaseOutDTO baseOutDTO = new BaseOutDTO();
		try {

			return prePurchaseService.doEditPrePurActivity(prePurInDTO);

		} catch (ApplicationException ae) {
			baseOutDTO.setErrorCode(ae.errCode, ae.errMsg);
			ae.printStackTrace();
		} catch (Exception e) {
			baseOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
			e.printStackTrace();
		}
		return baseOutDTO;
	}

	// 预采购活动 添加
		@RequestMapping(value = "/addBalance")
		@ResponseBody
		public BaseOutDTO addBalance(DoAddPrePurchBalanceInDTO doAddPrePurchBalanceInDTO) {
			BaseOutDTO baseOutDTO = new BaseOutDTO();
			try {

				return prePurchaseService.finishAddPrePurBalance(doAddPrePurchBalanceInDTO);

			} catch (ApplicationException ae) {
				baseOutDTO.setErrorCode(ae.errCode, ae.errMsg);
				ae.printStackTrace();
			} catch (Exception e) {
				baseOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
				e.printStackTrace();
			}
			return baseOutDTO;
		}
	
	// 预采购活动 删除
	@RequestMapping(value = "/deletePrePurActivity")
	@ResponseBody
	public BaseOutDTO deletePrePurActivity(PrePurInDTO prePurInDTO) {
		BaseOutDTO baseOutDTO = new BaseOutDTO();
		try {

			Boolean flag = prePurchaseService.deletePrePurActivity(prePurInDTO);
			if (flag) {
				baseOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
				return baseOutDTO;
			} else {
				baseOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理失败");
			}

		} catch (ApplicationException ae) {
			baseOutDTO.setErrorCode(ae.errCode, ae.errMsg);
			ae.printStackTrace();
		} catch (Exception e) {
			baseOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
			e.printStackTrace();
		}
		return baseOutDTO;
	}

	// 预采购活动 结束
	@RequestMapping(value = "/endPrePurActivity")
	@ResponseBody
	public BaseOutDTO endPrePurActivity(PrePurInDTO prePurInDTO) {
		BaseOutDTO baseOutDTO = new BaseOutDTO();
		try {

			return prePurchaseService.finishEndPrePurActivity(prePurInDTO);
			

		} catch (ApplicationException ae) {
			baseOutDTO.setErrorCode(ae.errCode, ae.errMsg);
			ae.printStackTrace();
		} catch (Exception e) {
			baseOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
			e.printStackTrace();
		}
		return baseOutDTO;
	}

	// 预采购活动 用户支付优惠比例比例
	@RequestMapping(value = "/updatePrePurDiscount")
	@ResponseBody
	public BaseOutDTO updatePrePurUserDiscount(UpdateInPrepurInDTO updateInPrepurInDTO) {
		BaseOutDTO baseOutDTO = new BaseOutDTO();
		try {

			Boolean flag = prePurchaseService.updatePrePurUserDiscount(updateInPrepurInDTO);
			if (flag) {
				baseOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
				return baseOutDTO;
			} else {
				baseOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理失败");
			}

		} catch (ApplicationException ae) {
			baseOutDTO.setErrorCode(ae.errCode, ae.errMsg);
			ae.printStackTrace();
		} catch (Exception e) {
			baseOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
			e.printStackTrace();
		}
		return baseOutDTO;
	}

	// 预采购活动 查询
	@RequestMapping(value = "/listPrePurActivity")
	@ResponseBody
	public BaseOutDTO listPrePurActivity(PrePurInDTO prePurInDTO, PageDTO pageDTO) {
		BaseOutDTO baseOutDTO = new BaseOutDTO();
		try {

			Page page = Tools.pageEmtity(pageDTO);
			PrePurActivityListOutDTO prePurActivityListOutDTO = prePurchaseService.listPrePurActivity(prePurInDTO, page);
			prePurActivityListOutDTO.setPage(page);
			prePurActivityListOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
			return prePurActivityListOutDTO;

		} catch (ApplicationException ae) {
			baseOutDTO.setErrorCode(ae.errCode, ae.errMsg);
			ae.printStackTrace();
		} catch (Exception e) {
			baseOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
			e.printStackTrace();
		}
		return baseOutDTO;
	}

	// 预采购活动 查询
	@RequestMapping(value = "/prePurActivityApprove")
	@ResponseBody
	public BaseOutDTO prePurActivityApprove(PrePurApproveInDTO prePurInDTO) {
		BaseOutDTO baseOutDTO = new BaseOutDTO();
		try {

			Boolean flag = prePurchaseService.finishPrePurApprove(prePurInDTO);
			if (flag) {
				baseOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
				return baseOutDTO;
			} else {
				baseOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理失败");
			}

		} catch (ApplicationException ae) {
			baseOutDTO.setErrorCode(ae.errCode, ae.errMsg);
			ae.printStackTrace();
		} catch (Exception e) {
			baseOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
			e.printStackTrace();
		}
		return baseOutDTO;
	}

	// 预采购余额变化记录
	@RequestMapping(value = "/listPreBalanceChange")
	@ResponseBody
	public BaseOutDTO listPreBalanceChange(PreBalanceChangeInDTO changeInDTO, PageDTO pageDTO) {
		BaseOutDTO baseOutDTO = new BaseOutDTO();
		try {

			Page page = Tools.pageEmtity(pageDTO);
			PreBalanceChangeListOutDTO preBalanceChangeListOutDTO = prePurchaseService.listPreBalanceChange(changeInDTO, page);
			preBalanceChangeListOutDTO.setPage(page);
			preBalanceChangeListOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
			return preBalanceChangeListOutDTO;

		} catch (ApplicationException ae) {
			baseOutDTO.setErrorCode(ae.errCode, ae.errMsg);
			ae.printStackTrace();
		} catch (Exception e) {
			baseOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
			e.printStackTrace();
		}
		return baseOutDTO;
	}

	// 预采购余额变化记录
	@RequestMapping(value = "/listPreBalanceUse")
	@ResponseBody
	public BaseOutDTO listPreBalanceUse(PreBalanceChangeInDTO changeInDTO, PageDTO pageDTO) {
		BaseOutDTO baseOutDTO = new BaseOutDTO();
		try {

			Page page = Tools.pageEmtity(pageDTO);
			PreBalanceChangeListOutDTO preBalanceChangeListOutDTO = prePurchaseService.listPreBalanceUse(changeInDTO, page);
			preBalanceChangeListOutDTO.setPage(page);
			preBalanceChangeListOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
			return preBalanceChangeListOutDTO;

		} catch (ApplicationException ae) {
			baseOutDTO.setErrorCode(ae.errCode, ae.errMsg);
			ae.printStackTrace();
		} catch (Exception e) {
			baseOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
			e.printStackTrace();
		}
		return baseOutDTO;
	}
	
	// 预采购活动 统计查询
	@RequestMapping(value = "/prePurActivityStatic")
	@ResponseBody
	public BaseOutDTO prePurActivityStatic(PrePurInDTO prePurInDTO, PageDTO pageDTO) {
		BaseOutDTO baseOutDTO = new BaseOutDTO();
		try {
			Page page = Tools.pageEmtity(pageDTO);
			PrePurActivityStaticListOutDTO prePurActivityStaticListOutDTO= prePurchaseService.listPrePurActivityStatic(prePurInDTO,page);
			prePurActivityStaticListOutDTO.setPage(page);
			prePurActivityStaticListOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
			return prePurActivityStaticListOutDTO;
		} catch (ApplicationException ae) {
			baseOutDTO.setErrorCode(ae.errCode, ae.errMsg);
			ae.printStackTrace();
		} catch (Exception e) {
			baseOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
			e.printStackTrace();
		}
		return baseOutDTO;
	}

	// 预采购活动 查询
		@RequestMapping(value = "/shopPrePurchaseActivity")
		@ResponseBody
		public BaseOutDTO shopPrePurchaseActivity(PrePurInDTO prePurInDTO, PageDTO pageDTO) {
			BaseOutDTO baseOutDTO = new BaseOutDTO();
			try {
				Page page = Tools.pageEmtity(pageDTO);
				ShopPrePurListOutDTO shopPrePurListOutDTO= prePurchaseService.findShopPrePurActivityStaic(prePurInDTO,page);
				shopPrePurListOutDTO.setPage(page);
				shopPrePurListOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
				return shopPrePurListOutDTO;
			} catch (ApplicationException ae) {
				baseOutDTO.setErrorCode(ae.errCode, ae.errMsg);
				ae.printStackTrace();
			} catch (Exception e) {
				baseOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
				e.printStackTrace();
			}
			return baseOutDTO;
		}
	
	
	// 预采购活动 查询
	@RequestMapping(value = "/prePurActivityInfo")
	@ResponseBody
	public BaseOutDTO prePurActivityInfo(PrepurchaseInfoInDTO shopInDTO) {
		BaseOutDTO baseOutDTO = new BaseOutDTO();
		try {

			return prePurchaseService.prePurActivityInfo(shopInDTO);

		} catch (ApplicationException ae) {
			baseOutDTO.setErrorCode(ae.errCode, ae.errMsg);
			ae.printStackTrace();
		} catch (Exception e) {
			baseOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
			e.printStackTrace();
		}
		return baseOutDTO;
	}
	
	
	// 预采购活动 查询
		@RequestMapping(value = "/prePurActivityMoreInfo")
		@ResponseBody
		public BaseOutDTO prePurActivityMoreInfo(PrepurchaseInfoInDTO prepurchaseInfoInDTO, PageDTO pageDTO) {
			BaseOutDTO baseOutDTO = new BaseOutDTO();
			try {
				Page page = Tools.pageEmtity(pageDTO);
				PrePurActivityMoreInfoOutDTO prePurActivityMoreInfoOutDTO= prePurchaseService.prePurActivityMoreInfo(prepurchaseInfoInDTO,page);
				prePurActivityMoreInfoOutDTO.setPage(page);
				if(prePurActivityMoreInfoOutDTO.getErrorCode()==null){
					prePurActivityMoreInfoOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
				}
				return prePurActivityMoreInfoOutDTO;
			} catch (ApplicationException ae) {
				baseOutDTO.setErrorCode(ae.errCode, ae.errMsg);
				ae.printStackTrace();
			} catch (Exception e) {
				baseOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
				e.printStackTrace();
			}
			return baseOutDTO;
		}
		
		// 预采购活动 查询
				@RequestMapping(value = "/prePurchTrades")
				@ResponseBody
				public BaseOutDTO prePurchTrades(PrepurchaseInfoInDTO prepurchaseInfoInDTO) {
					BaseOutDTO baseOutDTO = new BaseOutDTO();
					try {
						
						PreTradeOutDTO preTradeOutDTO= prePurchaseService.prePurchTrades(prepurchaseInfoInDTO);
						
						if(preTradeOutDTO.getErrorCode()==null){
							preTradeOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
						}
						return preTradeOutDTO;
					} catch (ApplicationException ae) {
						baseOutDTO.setErrorCode(ae.errCode, ae.errMsg);
						ae.printStackTrace();
					} catch (Exception e) {
						baseOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
						e.printStackTrace();
					}
					return baseOutDTO;
				}

		
	
}
